package com.ruoyi.app.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.app.domain.SysVersion;
import com.ruoyi.app.service.ISysVersionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 版本管理Controller
 * 
 * @author ruoyi
 * @date 2025-04-13
 */
@RestController
@RequestMapping("/system/version")
public class SysVersionController extends BaseController
{
    @Autowired
    private ISysVersionService sysVersionService;

    /**
     * 查询用于记录软件、文档等版本相关信息的列表
     */
    @PreAuthorize("@ss.hasPermi('system:version:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysVersion sysVersion)
    {
        startPage();
        List<SysVersion> list = sysVersionService.selectSysVersionList(sysVersion);
        return getDataTable(list);
    }

    /**
     * 导出版本列表
     */
    @PreAuthorize("@ss.hasPermi('system:version:export')")
    @Log(title = "版本管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysVersion sysVersion)
    {
        List<SysVersion> list = sysVersionService.selectSysVersionList(sysVersion);
        ExcelUtil<SysVersion> util = new ExcelUtil<SysVersion>(SysVersion.class);
        util.exportExcel(response, list, "版本数据");
    }

    /**
     * 获取版本详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:version:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(sysVersionService.selectSysVersionById(id));
    }

    /**
     * 新增版本
     */
    @PreAuthorize("@ss.hasPermi('system:version:add')")
    @Log(title = "版本管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysVersion sysVersion)
    {
        sysVersion.setDelFlag("0");
        return toAjax(sysVersionService.save(sysVersion));
    }

    /**
     * 修改版本
     */
    @PreAuthorize("@ss.hasPermi('system:version:edit')")
    @Log(title = "版本管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysVersion sysVersion)
    {
        return toAjax(sysVersionService.updateById(sysVersion));
    }

    /**
     * 删除版本
     */
    @PreAuthorize("@ss.hasPermi('system:version:remove')")
    @Log(title = "版本管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        List<String> objects = Arrays.asList(ids);
        return toAjax(sysVersionService.removeByIds(objects));
    }

    /**
     * 获取当前版本
     */
//    @PreAuthorize("@ss.hasPermi('system:version:query')")
    @GetMapping(value = "/getCurrentVersion")
    public AjaxResult getCurrentVersion()
    {
        return success(sysVersionService.getCurrentVersion());
    }
}
